package server.business.login.handler;

import server.ScriptEnum;
import server.ServerEngin;
import server.ServerHandler;
import server.business.login.ILoginScript;
import server.commonData.Information;
import server.commonData.Robot;
import server.commonData.RobotManager;
import server.commonData.Room;
import server.commonData.Table;
import server.commonData.User;
import server.msgBean.LoginMessage.EnterTableReq;

import com.core.initialize.Message;
import com.google.protobuf.GeneratedMessage;
/**
 * 请求进入桌子
 * @author King
 *
 */
public class EnterTableHandler extends ServerHandler {

	@Override
	public Class<? extends GeneratedMessage> initBodyClass()
	{
		return EnterTableReq.class;
	}

	@Override
	protected void handler(Message msg, User user) throws Throwable
	{
		EnterTableReq req = msg.getBody();
		if(user==null)
			return;
		Room room = ServerEngin.getInstance().getCache().getRoomManager().getRoom(req.getRoomName());
		if(room==null)
			return;
		ILoginScript scripte = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.LOGIN_SCRIPT);
		if(user.getTableId()!=0)
		{
			scripte.leftTable(user);
			System.err.println("玩家已经在桌子上面了");
			return;
		}
		
		if(user.getMoney()<room.getBeilv())
		{
			System.err.println("金币不够:"+room.getBeilv());
			return;
		}
		//判断是否已经在桌子中了
		if(user.getTableId()!=0)
			return;
		Table table = scripte.joinTable(room, user);
		//TODO 加入机器人
		Robot robot = RobotManager.createRobot();
		room.joinTable(table, robot);
	}
}
